<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="表单名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入表单名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleEditTable"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="表单名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
        width="130"
      />
      <el-table-column
        label="表单标识符"
        align="center"
        prop="formKey"
        :show-overflow-tooltip="true"
        width="130"
      />
      <el-table-column
        label="数据表名"
        align="center"
        prop="tableName"
        :show-overflow-tooltip="true"
        width="130"
      />
      <el-table-column
        label="表单描述"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"

      />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-view"
            @click="handlePreview(scope.row)"
          >预览</el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            @click="handleEditTable(scope.row)"
          >编辑</el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :title="`表单预览 —— ${formName}`"
      :visible.sync="previewForm"
      width="30%"
      :before-close="handleClose">
      <Preview :formJson="formJson"></Preview>
    </el-dialog>

  </div>
</template>

<script>
import Preview from '@/views/form/Preview'
import { getForm, deleForm } from '@/api/form/vform'
export default {
  name: "FormManager",
  components: {
    Preview
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表数据
      tableList: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined
      },
      previewForm: false,
      formJson: {},
      formName: ''
    };
  },
  created() {
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange() {

    },
    /** 表单管理 -- 增删改查 */
    getList() {
      this.loading = true;
      getForm(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
        this.loading = false
        this.tableList = res.rows
        this.total = res.total
      }).catch(() => {
        this.loading = false
      })
    },
    handleDelete(data) {
      deleForm(data.id).then(res => {
        this.loading = false
        this.$message({
          type: 'success',
          message: res.msg
        })
        this.getForm()
      }).catch(() => {
        this.loading = false
      })
    },

    /** 表单预览及关闭 */
    handlePreview(data) {
      this.previewForm = true
      this.formJson = JSON.parse(data.vform)
      this.formName = data.name
    },
    handleEditTable(data) {
      this.$router.push({
        name: 'VForm',
        params:{ data }
      })
    },
    handleClose() {
      this.previewForm = false
    }
  }
};
</script>


<style lang="scss" scoped>

</style>
<style></style>



